import os
import cv2
from PIL import Image
import pickle
import numpy as np
# from .CImage import CImage
# from .MyJson import JsonObject
# import myImage.CImage as CImage
import myImage.MyJson as MyJson

fThresh = 0.10


def fileparts(fileName):
    [dirName, fName] = os.path.split(fileName)
    [pureName, extName] = os.path.splitext(fName)
    return (dirName, pureName, extName, fName)


def IsInRect(box, rect):
    vUp = np.array([rect[2], rect[2], rect[3], rect[3]])
    vDown = np.array([rect[0], rect[0], rect[1], rect[1]])
    box = np.array(box)

    ins1 = box <= vUp
    ins2 = box >= vDown

    bIn = not (False in (ins1 & ins2))
    return bIn


## box = [xl, xr, yl, yr]
## rect = [xl, yl, xr, yr]
# 两者坐标不对
import math


def IsBoxMostInRect(box, rect2):
    box = np.array(box)
    rect2 = np.array(rect2)

    ins = [0, 2, 1, 3]
    rect = rect2[ins]

    ratio = 0
    inBox = np.zeros(box.size)
    inBox[0: 3: 2] = np.maximum(box[0: 3: 2], rect[0: 3: 2])
    inBox[1: 4: 2] = np.minimum(box[1: 4: 2], rect[1: 4: 2])

    area1 = calcArea(box)
    area2 = calcArea(inBox)

    if area2 < 0:
        bIn = False
        return bIn, inBox, ratio

    ratio = area2 / area1
    if ratio > fThresh:
        bIn = True
    else:
        bIn = False
    return bIn, inBox, ratio


def calcArea(box):
    # print(box)
    ww = box[1] - box[0]  ##后面要加1，包括Matlab
    hh = box[3] - box[2]

    flag = ww > 0 and hh > 0
    area = (abs(ww) + 1) * (abs(hh) + 1)
    if flag == False:
        area = area * -1

    return area


def calcArea2(box):  ##前两个是小坐标，后两个是大作表
    ww = abs(box[1] - box[0]) + 1  ##后面要加1，包括Matlab
    hh = abs(box[3] - box[2]) + 1

    return ww * hh



def setYoloPath(func):
    import inspect
    yoloPath = os.path.abspath(inspect.getfile(func))
    [yoloDir, _] = os.path.split(yoloPath)
    cwd = os.getcwd()
    os.chdir(yoloDir)



import matplotlib.pyplot as plot

if __name__ == "__main__":

    pass









    # im = Image.open(r"F:\玉溪烟草\matlab\detect_all\detect_all-2022-09-24\97w.jpg")
    # # 通道分离
    # rgb = im.split()
    # print(rgb.shape)
    # R, G, B = 0, 1, 2
    # # 使用 lambda 表达式实现像素级操作
    # rgb[R].paste(rgb[R].point(lambda x: x * 0.9))
    # rgb[G].paste(rgb[G].point(lambda x: x * 0.9))
    # rgb[B].paste(rgb[B].point(lambda x: x * 0.75))
    # plot.subplot(1, 2, 1)
    # plot.imshow(im)
    # plot.subplot(1, 2, 2)
    # plot.imshow(Image.merge(im.mode, rgb))
    # plot.show()

'''
 1
 cv2读取图像通道为    BGR
 cv2读取图像尺寸为    HWC

 2
 PIL读取图像通道为    RGB
 PIL读取图像尺寸为    WHC
'''

import time
class MyTimer(object):
    start = 0

    def __init__(self, showString = True):
        self.start = time.perf_counter()
        self.showString = showString

    # 打印计时器
    def printRunSeconds(self, echo, isSetNewStart = True):
        if self.showString == False:
            return

        end = time.perf_counter()
        duration = end - self.start

        print(echo, duration)

        if isSetNewStart == True:
            self.start = time.perf_counter()

    @staticmethod
    def initStart():
        MyTimer.start = time.perf_counter()

    @staticmethod
    def printSeconds(echo, isSetNewStart = False):
        end = time.perf_counter()
        duration = end - MyTimer.start
        print(echo, duration)

        if isSetNewStart == True:
            MyTimer.start = time.perf_counter()
